<?php 
/*
 module:		定时任务控制器
 create_time:	2022-08-19 10:13:15
 author:		
 contact:		
*/

namespace app\Controller\admin;
use function Hyperf\ViewEngine\view;
use App\Exception\ValidateException;
use App\Model\admin\CrontabModel;
use App\Controller\admin\CommonController;
use Hyperf\DbConnection\Db;

class CrontabController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->isMethod('get')){
			return view('admin/crontab/index');
		}else{
			$limit  = $this->request->input('limit', 20);
			$page = $this->request->input('page', 1);

			$order  = $this->request->input('order');	//排序方式
			$sort  = $this->request->input('sort');		//排序字段

			$where = [];
			$where['crontab_id'] = $this->request->input('crontab_id');
			$where['name'] = $this->request->input('name');
			$where['type'] = $this->request->input('type');
			$where['status'] = $this->request->input('status');

			$sort = !empty($sort) ? $sort : 'crontab_id';
			$order = !empty($order) ? $order : 'desc';

			$field = 'crontab_id,name,type,rule,status,target,last_run_time';

			$query = CrontabModel::select(explode(",",$field));

			$res = formatWhere($query,$where)->orderBy($sort,$order)->paginate($limit)->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'crontab_id,status';
		$data = $this->request->inputs(explode(',',$postField),'post');
		if(!$data['crontab_id']) throw new ValidateException ('参数错误');
		CrontabModel::whereIn('crontab_id',explode(',',$data['crontab_id']))->update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'name,type,rule,status,target,remark';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\CrontabValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			$res = CrontabModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'crontab_id,name,type,rule,status,target,remark';
		$data = $this->request->inputs(explode(',',$postField),'post');

		$validate = new \App\Validate\admin\CrontabValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			CrontabModel::where('crontab_id',$data['crontab_id'])->update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->input('crontab_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'crontab_id,name,type,rule,status,target,remark';
		$res = CrontabModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->input('crontab_id');
		if(!$idx) throw new ValidateException ('参数错误');
		CrontabModel::whereIn('crontab_id',explode(',',$idx))->delete();
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->input('crontab_id');
		if(!$id) throw new ValidateException ('参数错误');

		$field = 'crontab_id,name,type,rule,status,target,last_run_time';

		$res = CrontabModel::select(explode(",",$field))->find($id);
		return json(['status'=>200,'data'=>$res]);
	}






}



